<?php
namespace jy_liangoods;
defined('BY_JYA') or exit('error');
/*
*/
class merch extends \plugin{
	
	/*
		商户
	*/
	public function checklogin($merchid=0){
		$merchid=$merchid?:$_SESSION['merchid'];
		if(!$_SESSION['uid']){
			$url=custom_url('jy_liangoods/login/',true);
			exi('请先登录','error',$url);
		}
		if($merchid){
			if(!$_SESSION['workertype']){
				$mid = pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid),'mid');
				if(!$mid || $mid !=$_SESSION['uid']){
					unset($_SESSION['merchid']);
					$merchid=0;
				}
			}else{
				$mid = pdo_getcolumn('jy_merch_worker',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'mid'=>$_SESSION['uid']),'mid');
				if(!$mid){
					unset($_SESSION['merchid']);
					$merchid=0;
				}
			}
		}
		if(!$merchid){
			$merchid = pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
			if($merchid){
				unset($_SESSION['workertype']);
			}
		}
		if(!$merchid){
			$worker=pdo_get('jy_merch_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			if($worker['type']){
				$merchid = $worker['merchid'];
				$_SESSION['workertype']=$worker['type'];
			}
		}
		if(!$merchid){
			$url=custom_url('jy_liangoods/login/',true);
			exi('请先登录','error',$url);
		}
		return $merchid;
	}
	/*
		生成订单$order:商城订单
	*/
	public function add_sendlog($order){
		$order_id=intval($order['id']);
		if($order['sendtype']!=1){
			if($order['storeid']){
				$this->model('notice')->notice_storeorder($order);
			}
			return false;
		}
		$order=model('jy_weishop/order')->get_order($order_id);
		$allgoods=array();
		$allprice=0;
		foreach($order['goods'] as $item){
			$pcate=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']),'pcate');
			$cate_id=pdo_getcolumn('jy_merch_category',array('uniacid'=>$_SESSION['uniacid'],'pcate'=>$pcate),'id');
			$goodsinfo=$item['goodsinfo']?json_decode($item['goodsinfo'],true):array();
			$goods=array();
			if(!$allgoods[$cate_id]){
				$goods[]=$item;
				$allgoods[$cate_id]=array(
					'order_id'=>$order['id'],
					'count'=>1,
					'goodsprice'=>$item['total_price'],
					'costprice'=>$goodsinfo['costprice']*$item['total'],
					'goods'=>$goods,
				);
			}else{
				$allgoods[$cate_id]['goods'][]=$item;
				$allgoods[$cate_id]['goodsprice'] +=$item['total_price'];
				$allgoods[$cate_id]['costprice'] +=$goodsinfo['costprice']*$item['total'];
				$allgoods[$cate_id]['count'] +=1;
			}
			$allprice +=$item['total_price'];
		}
		$order['address']=json_decode($order['address'],true);
		if($order['address'] && $order['address']['name']){
			$address=$order['address'];
		}else{
			$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
		}
		$count=count($order['goods']);
		$duofee=$order['price']-$allprice;
		if($duofee && $count){
			$oneduofee=$duofee/$count;
		}
        $issendmids=array();
		$basic=$this->plugin_setting('basic');
		foreach($allgoods as $k=>$ag){
			if($oneduofee){
				$up_price=$ag['count']*$oneduofee;
			}
			$price=$up_price?$ag['goodsprice']+$up_price:$ag['goodsprice'];
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'storeid'=>$order['storeid'],
				'order_id'=>$order['id'],
				'buymid'=>$order['mid'],
				'goods'=>ijson_encode($ag['goods']),
				'cate_id'=>$k,
				'name'=>$order['realname']?:$address['name'],
				'mobile'=>$order['mobile']?:$address['mobile'],
				'pcd'=>ijson_encode($address['pcd']),
				'address'=>$address['address'],
				'sendtype'=>$order['sendtype'],
				'goodsprice'=>$ag['goodsprice'],
				'costprice'=>$ag['costprice'],
				'price'=>$price,
				'up_price'=>$up_price,
				'createtime'=>TIMESTAMP,
				'status'=>0,
			);
			$this->insert('sendlog',$data);
			$log_id=pdo_insertid();
			if($basic['is_newget']){
				$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'cateid2 like'=>'%;'.$data['cate_id'].';%');
				$merchs=pdo_getall('jy_merch_user',$params,array('id','mid','status'));
				//file_put_contents(ROOT_D.'/1.txt',"131\n");
				foreach ($merchs as $mv){
					if($issendmids[$mv['mid']]){
						//先注销，容易测试误解
						continue;
					}
					$issendmids[$mv['mid']]=1;
					$this->model('notice')->notice_newget($log_id,$mv['mid']);
				}
			}
		}
		return true;
	}
	/*
		取消订单$order:商城订单
	*/
	public function cancelorder($order){
		$sendlogs=pdo_getall('jy_liangoods_sendlog',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$order['id']));
		foreach($sendlogs as $sv){
			if($sv['merchid']){
				$this->model('notice')->notice_cancelorder($sv);
			}
		}
		pdo_update('jy_liangoods_sendlog',array('status'=>7),array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$order['id']));
		return true;
	}
	/*
		抢单
	*/
	public function order_get($params){
		$log_id=intval($params['log_id']);
		$merchid=intval($params['merchid']);
		$isget=pdo_getcolumn('jy_liangoods_sendlog',array('id'=>$log_id),'status');
		if($isget==1){
			exi('该订单已被别人抢单了！','error');
		}
		pdo_update('jy_liangoods_sendlog',array('merchid'=>$merchid,'status'=>1,'gettime'=>time()),array('id'=>$log_id));
		$this->model('notice')->notice_orderget($log_id);
		return true;
	}
	/*
		完成订单
	*/
	public function order_receive($log_id){
		pdo_update('jy_liangoods_sendlog',array('status'=>4),array('id'=>$log_id));
		return true;
	}
	/*
		完成订单
	*/
	public function order_complete($order){
		return true;
	}
	public function check_sendlog($log_id,$status=0,$remark=''){
		if($status){
			$status=$status==1?3:6;
			pdo_update('jy_liangoods_sendlog',array('status'=>$status,'remark'=>$remark),array('id'=>$log_id));
			if($status==3){
				$this->model('notice')->notice_status3($log_id);
			}
			if($status==6){
				$this->model('notice')->notice_status6($log_id);
			}
		}
		return true;
	}
	public function order_send($log_id,$data=array(),$express_type=0){
		if(!$express_type){
			pdo_update('jy_liangoods_sendlog',array('status'=>4,'sendtime'=>time(),'express'=>$data['express'],'expresssn'=>$data['expresssn']),array('id'=>$log_id));
		}else{
			pdo_update('jy_liangoods_sendlog',array('status'=>4,'sendtime'=>time()),array('id'=>$log_id));
		}
		$order_id=pdo_getcolumn('jy_liangoods_sendlog',array('id'=>$log_id),'order_id');
		model('jy_weishop/order')->order_send($order_id,$data);
		return true;
	}
	/*
		订单升级为待结算
	*/
	public function cash_sendlog($merchid=0,$fee=0,$pay_type=''){
		$cashdays=$this->setting['cash_day']?:1;
		$cashtime = mktime(0,0,0,date('m'),date('d')-$cashdays,date('Y'));
		pdo_update('jy_liangoods_sendlog',array('clearstatus'=>1),array('clearstatus'=>0,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'status'=>4));
		pdo_update('jy_liangoods_balance',array('status'=>4),array('status'=>3,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid));
		$mid = pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid),'mid');
		if($mid){
			$msg='我们已受理您的”线上打款“申请，如有疑问请联系平台客服！';
			if($pay_type=='credit'){
				$msg='打款申请已受理，请打开会员中心留意账户余额变化';
			}
			if($pay_type=='downline'){
				$msg='我们已受理您的”线下微信打款“申请，如有疑问请联系平台客服';
			}
			$this->model('notice')->notice_fenxiao_dakuan($mid,$fee,$msg);
		}
		return true;
	}
	function get_choufee($ordermoney,$merchid=0,$isup=0,$costprice=0){
		if($ordermoney<=0){
			return false;
		}
		$merchsfee=array();
		$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid));
		$merchsfee[$merchid]=array(
					'id'=>$merchid,
					'choufee_set'=>$merch['choufee_set']?json_decode($merch['choufee_set'],true):array(),
					'goodsfee'=>$merchsfee[$merchid]['goodsfee']+$ordermoney,
					'out_upfee'=>$merch['out_upfee'],
		);
		$merchnum=count($merchsfee);
		$choufee_set=plugin('jy_merch')->plugin_setting('setting');
		$commission_type=$choufee_set['shopcommission_type'];
		if($commission_type==3 && $merch['choufee_set'] && ($merch['choufee_set']['shoprate'] || $merch['choufee_set']['shoprateyuan'] || $merch['choufee_set']['shopratejieti'])){
			$choufee_set=$merch['choufee_set'];
			$commission_type=$choufee_set['shopcommission_type'];
		}
		$choufee_set['rate']=$choufee_set['shoprate']?:$choufee_set['rate'];
		$choufee_set['rateyuan']=$choufee_set['shoprateyuan'];
		$choufee_set['ratejieti']=$choufee_set['shopratejieti'];
		$commission_totalfee=$ordermoney;
		if(!$merch['choufee_set'] || $commission_type==0){
			$commission_fee =$commission_totalfee*$choufee_set['rate']/100;
		}elseif($commission_type==1){
			$commission_fee =$choufee_set['rateyuan'];
		}elseif($commission_type==2){
			$minrate=20;
			foreach($choufee_set['ratejieti'] as $rv){
				if(!$rv['rate'] || $rv['fee']>$commission_totalfee){
					continue;
				}
				$minrate=$rv['rate']>$minrate?$minrate:$rv['rate'];
			}
			$commission_fee =$commission_totalfee*$minrate/100;
		}elseif($commission_type==3){
			$commission_fee =$commission_totalfee*$choufee_set['rate']/100;
		}elseif($commission_type==4){
			$commission_fee =$commission_totalfee-$costprice;
		}
		//返回最后一个商户的
		$merchorder=array(
			'status'=>0,
			'commission_type'=>$commission_type,
			'commission_price'=>$commission_totalfee,
			'commission_money'=>intval($commission_fee*100)/100,
		);
		return $merchorder;
	}
}